<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Video extends MY_Controller {
	
	public function __construct()
	{
		parent::__construct();
		
		if (stristr($_SERVER['PHP_SELF'], 'video/watch') == -1)
		{
			$this->check_authentication(2);
		}
		
		$this->load->model('Company_video_model', 'video');
		$this->lang->load('company/video', $this->session->userdata['user_lang']);
	}
	
	public function index()
	{
		$videos_list = $this->video->get(array('idmember' => $this->session->userdata['idmember']));
		
		$grid_videos = array();
        
		foreach ($videos_list->result() as $row)
		{
			$type = $row->idopentok == 'Y' ? 'opentok' : 'youtube';
			$highlight = $row->idpresentation == 'Y' ? ' row-highlight' : '';
			$title_additional = $row->idpresentation == 'Y' ? (' (' . $this->lang->line('video_presentation') . ')') : '';
			
			$video = '<div class="row'.$highlight.'"><span><span class="img-galery"><span class="img-border fleft vmiddle">';
			
			if ($row->idopentok != 'Y')
			{
				$video .= '<img class="pointer" width="160" height="120" onclick="CompanyVideo.watchVideo(\''.$type.'\', \''.$row->link.'\');" src="http://img.youtube.com/vi/'.$row->link.'/0.jpg" />';
			}
			else
			{
				$video .= '<img class="pointer" onclick="CompanyVideo.watchVideo(\''.$type.'\', \''.$row->link.'\');" src="'.base_url() . "f/" . $this->session->userdata['idmember'] . '/vid/' . $row->image.'" />';
			}
			
			$video .= '</span></span><span class="fleft vmiddle box-description"><h3>'.$row->title . $title_additional .'</h3><p>'.nl2br($row->description).'</p></span>
			</span>
			<div class="gtool">
			<a onclick="CompanyVideo.watchVideo(\''.$type.'\', \''.$row->link.'\');">'.$this->lang->line('video_watchvideo').'</a>';

			if ($row->idpresentation != 'Y')
			{
				$video .= '&middot; <a onclick="CompanyVideo.updatePresentation('.$row->id.');">'.$this->lang->line('video_updatepresentation').'</a>';
			}
			
			 $video .= '&middot; <a onclick="CompanyVideo.editVideo(\''.$type.'\', '.$row->id.');">'.$this->lang->line('video_updatetext').'</a>
			 &middot; <a onclick="CompanyVideo.deleteVideo('.$row->id.');">'.$this->lang->line('global_delete').'</a></div></div>';
			
			$grid_videos[] = $video;
		}
		
		if (!$grid_videos) {
			 $data['grid_videos'] = '<center>'.$this->lang->line('video_noresults').'</center>';
		} else {
			 $data['grid_videos'] = implode('<hr/>', $grid_videos);
		}
		
		exitHTML($data['grid_videos']);
	}
	
	public function insert()
	{
		header("Content-type: text/html; charset=utf-8");
		$data = $_POST;
		
		if (missing_field($data, array('link')))
		{
			formError($this->lang->line('video_missingfield'), true);
		}
		
		if (isset($data['type']) && $data['type'] == 'youtube')
		{
			$data['link'] = valid_youtube($data['link']);
			
			if (!$data['link'])
			{
				formError($this->lang->line('video_invalidyoutubelink'), true);
			}
		}
		
		if (isset($data['image']) && $data['image'] != '')
		{
			$tmp_path = FCPATH . 'tmp/' . $this->session->userdata['key'] . '_capture';
			
			if (!is_dir($tmp_path))
			{
				mkdir($tmp_path);
			}
			
			$tmp_file = $tmp_path . '/image.png';
			file_put_contents($tmp_file, base64_decode($data['image']));
			
			$data['tmp_path'] = $tmp_path;
			$data['tmp_file'] = $tmp_file;
			$data['image_name'] = 'image.png';
		}
		
		$data['idmember'] = $this->session->userdata['idmember'];
		
		if ($this->video->insert($data) === false)
		{
		   formError($this->lang->line('global_systemfail'), true, true);
		}
		else
		{
			if ($data['type'] != 'youtube')
			{
				exit('<script type="text/javascript">
						parent.CompanyVideo.getVideoGrid();
						parent.closeBox();
					</script>');
			}
			else
			{
				exitJs('CompanyVideo.getVideoGrid();closeBox();');
			}
		}
	}
	
	public function update()
	{
		$data = $_POST;
		
		if (missing_field($data, array('idvideo')))
		{
			formError($this->lang->line('video_missingfield'), true);
		}
		
		$data['idmember'] = $this->session->userdata['idmember'];
		$data['id'] = $data['idvideo'];
		
		if ($this->video->update($data) === false)
		{
		   formError($this->lang->line('global_systemfail'), true);
		}
		else
		{
			exitJs('CompanyVideo.getVideoGrid();closeBox();');
		}
	}
	
	public function update_presentation($video_id = null)
	{
		if (!$video_id)
		{
			formError($this->lang->line('video_missingfield'), true);
		}
		
		$data['id'] = $video_id;
		
		if ($this->video->update_presentation($data) === false)
		{
		   formError($this->lang->line('global_systemfail'), true);
		}
		else
		{
			exitJs('CompanyVideo.getVideoGrid();');
		}
	}
	
	public function delete()
	{
		$data = $_POST;
		
		if (missing_field($data, array('id')))
		{
			formError($this->lang->line('video_missingfield'));
		}
		
		if ($this->video->delete(array('id' => $data['id'])) === false)
		{
		   formError($this->lang->line('global_systemfail'));
		}
		else
		{
			exitJs('CompanyVideo.getVideoGrid();');
		}
	}
	
	public function edit($video_type = 'youtube', $id_video = null)
	{
		header("Content-type: text/html; charset=utf-8");
		$data = $_POST;
		
		$data['id_video'] = $id_video;
		$data['title'] = '';
		$data['description'] = '';
		$data['link'] = '';
		$data['type'] = $video_type;
		
		if (is_numeric($id_video))
		{
			$video_data = $this->video->get(array('idvideo' => $id_video))->row();
			$data['title'] = $video_data->title;
			$data['description'] = $video_data->description;
			$data['link'] = $video_data->link;
		}
		
		$data['userdata'] = $this->session->userdata;
        $this->load->view('company/video', $data);
	}
	
	public function add($type = 'youtube')
	{
		$this->edit($type, null);
	}
	
	public function capture()
	{
		header("Content-type: text/html; charset=utf-8");
		$data['userdata'] = $this->session->userdata;
        $this->load->view('company/video_capture', $data);
	}
	
	public function watch($video_type = 'youtube', $video_link = null)
	{
		header("Content-type: text/html; charset=utf-8");
		$data['userdata'] = $this->session->userdata;
		$data['video_link'] = $video_link;
		
		switch ($video_type)
		{
			case 'opentok':
        		
				$this->load->library("OpenTok/OpenTok");
		
				//arguments not necessary if you've set the API key in API_config.php in API_Config.php
				$OT = new OpenTok();
				
				$OT->generate_session_id();
				$OT->generate_token();
				
				$data['api_key'] = $OT->apiKey;
				$data['session_id'] = $OT->sessionId;
				$data['token'] = $OT->token;
				$data['archive_id'] = $video_link;
				
				$this->load->view('company/video_watch_opentok', $data);
        		break;
        		
			default:
				$this->load->view('company/video_watch_youtube', $data);
				break;
		}
	}
}

?>